<?php

if(!isset($_GET['pic'])|| !is_file($_GET['pic'])){ // datei nicht vorhanden oder keine angegeben
	echo "file not found, or no file as parameter";
	return;
}	
$pic=$_GET['pic'];	
if(!isset($_GET['width'])|| !isset($_GET['height'])){// keine breiten / hoehen angaben im link
	echo "no width and height values";
	return;
}	
$width=$_GET['width'];
$height=$_GET['height'];

header('Content-type: image/png'); // header setzen
$newFilename=strtolower(str_replace("._","thumb./",str_replace("/","_",$pic)))."w".$width."h".$height.".png";

if(is_file($newFilename)){ //thumb bereits gecached
	readfile($newFilename);
	return;
}	

function resizeImage($filename, $max_width, $max_height){
	list($orig_width, $orig_height,$typ) = getimagesize($filename);

	$width = $orig_width;
	$height = $orig_height;

	# taller
	if ($height > $max_height) {
		$width = ($max_height / $height) * $width;
		$height = $max_height;
	}

	# wider
	if ($width > $max_width) {
		$height = ($max_width / $width) * $height;
		$width = $max_width;
	}

	$image_p = imagecreatetruecolor($width, $height);
   
	switch ($typ) {
    
	 case 2:
		$image = imagecreatefromjpeg($filename);
		break;
	 case 3:
		$image = imagecreatefrompng($filename);
		break;
	 default:
		return;
	} 
   
	imagecopyresampled($image_p, $image, 0, 0, 0, 0,
                                     $width, $height, $orig_width, $orig_height);

	return $image_p;
}
$test = resizeImage($pic, $width, $height);  
imagepng($test,$newFilename,5); // abspeichern
imagepng($test);// ausgeben



 
